import java.util.Arrays;

/**
 * @program: LeetCode
 * @description: LeetCode : 31. 下一个排列
 * @author: WXY
 * @create: 2023-03-14 11:11
 * @Version 1.0
 **/
public class Num031_nextPermutation {
    public static void nextPermutation(int[] nums) {
        for (int i = nums.length - 1; i >= 0; i--) {
            for (int j = nums.length - 1; j > i; j--) {
                if (nums[i] < nums[j]) {
                    swap(nums, i, j);
                    Arrays.sort(nums, i + 1, nums.length);
                    return;
                }
            }
        }
        Arrays.sort(nums);
    }

    private static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
